package com.gdougms.mapper;

import com.gdougms.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from user")
    List<User> getUsers();

    // 添加用户
    @Insert("insert into user(name,number,id_card,password,role,email) values(#{name},#{number},#{id_card},#{password},#{role},#{email})")
    int insertUser(User user);

    /**@Title
     * @Description 查询是否存在该用户
     * @Param num：idcard 或者 number
     * @return com.gdougms.test.entity.User
     * @Author iceF
     * @Date 2021/6/19
    */
    @Select("select * FROM user where number= #{num} or id_card = #{num}")
    User getUserByNumberOrIDCard(String num);

    //查找是否存在id为num的用户
    @Select("select * from user where id=#{id}")
    User getIfUserById(int id);

    //登录验证
    @Select("select * FROM user where (number=#{num} or id_card = #{num}) and password = #{pwd}")
    User login(String num,String pwd);

    //游客注册
    @Insert("Insert into user(name,id_card,password,email,role) VALUES(#{name},#{id_card},#{password},#{email},5)")
    int register(String name,String id_card,String password,String email);

    //更改密码
    @Update("UPDATE user set password=#{password} where id_card=#{username} or number=#{username}")
    int updatePassword(String password,String username);

    //绑定邮箱
    @Update("UPDATE user set email=#{email} where number=#{number}")
    int bindEmail(String email,String number);

    //管理员获取所有普通用户
    @Select("select * from user where role=1 or role=2 or role=5")
    List<User> getUserList();

    //管理员删除用户
    @Update("update user set state=0 where id_card=#{username} or number=#{username}")
    int deleteUser(String username);

    //管理员增加用户 写在xml中
//    @Insert("insert into user(name,number,id_card,password,role) values(#{name},#{number},#{idCard},#{password},#{role})")
    int addUser(String name,String number,String idCard,String password,int role,String email);

    //管理员修改用户信息
    int updateUser(String name,String number,String idCard,String password,String email,int id);

    //判断用户是否未绑定邮箱
    @Select("select email from user where number = #{number}")
    String userIfBindEmail(String number);
}
